﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_QC_IPQC_ORDER
// * CreateDate    : 2012/4/21 21:07:11
// * Creater       : CodeSmith
// * LastChangeDate: 2012/4/21 21:07:11
// * LastChanger   : 2012/4/21 21:07:11
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Business.Mst;

namespace ESOL.Business.Chk
{
    /// <summary>
    /// QcIpqcOrder
    /// </summary>
    public partial class QcIpqcOrderBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objQcIpqcOrderEntity">Entity</param>
        /// <returns>Entity</returns>
        public static QcIpqcOrderEntity GetQcIpqcOrder(QcIpqcOrderEntity objQcIpqcOrderEntity)
        {
            return DataAccess.SelectSingle<QcIpqcOrderEntity>(objQcIpqcOrderEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objQcIpqcOrderEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertQcIpqcOrder(QcIpqcOrderEntity objQcIpqcOrderEntity)
        {
            return DataAccess.Insert(objQcIpqcOrderEntity);
        }

        public static bool InsertQcIpqcOrder(List<QcIpqcOrderEntity> objQcIpqcOrderEntityList)
        {
            return DataAccess.Insert(objQcIpqcOrderEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objQcIpqcOrderEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateQcIpqcOrder(QcIpqcOrderEntity objQcIpqcOrderEntity)
        {
            return DataAccess.Update(objQcIpqcOrderEntity);
        }

        public static bool UpdateQcIpqcOrder(List<QcIpqcOrderEntity> objQcIpqcOrderEntityList)
        {
            return DataAccess.Update<QcIpqcOrderEntity>(objQcIpqcOrderEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objQcIpqcOrderEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteQcIpqcOrder(QcIpqcOrderEntity objQcIpqcOrderEntity)
        {
            return DataAccess.Delete(objQcIpqcOrderEntity);
        }

        public static bool DeleteQcIpqcOrder(List<QcIpqcOrderEntity> objQcIpqcOrderEntityList)
        {
            return DataAccess.Delete<QcIpqcOrderEntity>(objQcIpqcOrderEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetQcIpqcOrderAll()
        {
            return DataAccess.SelectAllDataSet<QcIpqcOrderEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<QcIpqcOrderEntity> GetQcIpqcOrderAllList()
        {
            return DataAccess.SelectAll<QcIpqcOrderEntity>();
        }
        #endregion

        #region Functions
        public static bool Save(bool IsNew,QcIpqcOrderEntity entity,DataTable dt,List<QcIpqcOrderItemEntity> listDel)
        {
             using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (IsNew)
                    {
                        string code = "";
                        if (MstCodeRuleBs.GetCurrentOrderCode("CheckingOrder", out code, broker) == false)
                        {
                            broker.RollBack();
                            throw new Exception("自动生成质检单号失败！");
                        }
                        entity.OrderCode = code;
                        if (entity.ResultTag == 0)//无不良
                        {
                            entity.Status = "STATUS06";
                        }
                        DataAccess.Insert(entity, broker);
                    }
                    else
                    {
                        DataAccess.Update(entity, broker);
                    }

                    List<QcIpqcOrderItemEntity> insertList = new List<QcIpqcOrderItemEntity>();
                    List<QcIpqcOrderItemEntity> modifyList = new List<QcIpqcOrderItemEntity>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr.RowState == DataRowState.Added)
                        {
                            QcIpqcOrderItemEntity m = new QcIpqcOrderItemEntity(dr);
                            m.OrderId = entity.OrderId;
                            insertList.Add(m);
                        }
                        else if (dr.RowState == DataRowState.Modified)
                        {
                            QcIpqcOrderItemEntity m = new QcIpqcOrderItemEntity(dr);
                            modifyList.Add(m);
                        }
                    }
                    DataAccess.Delete<QcIpqcOrderItemEntity>(listDel, broker);
                    DataAccess.Update<QcIpqcOrderItemEntity>(modifyList, broker);
                    DataAccess.Insert<QcIpqcOrderItemEntity>(insertList, broker);


                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static DataTable GetDtAllByCondition(string sCondition, string sRowCounts, string sOrderBy)
        {
            string sql=@"select qio.*,ppoi.ORDER_ITEM_CODE from QC_IPQC_ORDER qio
left join PROD_PLAN_ORDER_ITEM ppoi on qio.PROD_PLAN_ORDER_ITEM_ID=ppoi.PLAN_ORDER_ITEM_ID
where 1=1 ";
            if (string.IsNullOrEmpty(sCondition) == false)
                sql += sCondition;
            if (string.IsNullOrEmpty(sOrderBy) == false)
                sql += sOrderBy;
            return DataAccess.SelectDataSet(sql).Tables[0];
        }

        public static bool ApproveNg(QcIpqcOrderEntity entity, List<QcIpqcOrderItemEntity> list)
        {
             using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Update(entity, broker);
                    DataAccess.Update<QcIpqcOrderItemEntity>(list, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
        #endregion
    }


}

